#!/usr/bin/env python
from flask import Flask,render_template,request
import os
from MyClass.chatbot import chatbot
from MyClass.voice import voice
#make access with ssl
ASSETS_DIR = os.path.dirname(os.path.abspath(__file__))

app = Flask(__name__)

@app.route('/')
def index():
    return 'hello worlld'
@app.route('/recognition/')
def recognition():
    return render_template('recognition.html')

@app.route('/record/')
def record():
    return render_template('record.html')

@app.route('/upload',methods=['POST'])
def upload():
    if request.method == 'POST':
        audiostring = request.form['data']
        filename = '/var/www/html/robot/audio.mp3'
        cd = chatbot()
        response = cd.saidToAnswer(audiostring)
        #response = cd.saidBySkill(audiostring)
        m = memory()
        result = m.getIntent(response)
        #juge if have memory in mysql
        if result == None:
            vc = voice()
            vc.voiceSpeech(filename,response)
            return response
        else:
            return result
        


if __name__=="__main__":
    context = ('/etc/apache2/ssl/apache.crt', '/etc/apache2/ssl/apache.key')#certificate and key files
    app.run(host='192.168.0.104',debug=True,ssl_context=context)